package com.cn.xact.ty_treasured_book.activity;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.activity.LoginActivity.MyNetworkCallBack;
import com.cn.xact.ty_treasured_book.net.NetworkCallBack;
import com.cn.xact.ty_treasured_book.net.NetworkImpl;
import com.cn.xact.ty_treasured_book.util.UIHelper;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MyCommentsActivity extends Activity {

    private EditText et_content;
    private NetworkImpl mPubCommentNetwork;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_my_comments);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_customer_title);

        WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();

        mPubCommentNetwork = new NetworkImpl(this, new MyNetworkCallBack());

        ImageView tmp = (ImageView)findViewById(R.id.common_title_indicator);
        tmp.setImageResource(R.drawable.arrow_left);
        TextView title1 = (TextView)findViewById(R.id.common_title_first_section);
        Button confirm = (Button)findViewById(R.id.common_title_button);
        confirm.setVisibility(View.VISIBLE);
        confirm.setText(getResources().getString(R.string.comments_send));
        confirm.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mPubCommentNetwork.publishComments("123", et_content.getText().toString());
            }

        });

        title1.setPadding(width/3, 0, 0, 0);
        title1.setText(R.string.comments_public);

        tmp.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                MyCommentsActivity.this.finish();
            }

        });

        final TextView tv_num;
        final int num=140;

        et_content=(EditText)findViewById(R.id.et_content);
        tv_num=(TextView)findViewById(R.id.tv_num);
        tv_num.setText(String.valueOf(num));


        et_content.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int selectionStart;
            private int selectionEnd;

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                temp = s;
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                int number = num - s.length();
                tv_num.setText("" + number);
                selectionStart = et_content.getSelectionStart();
                selectionEnd = et_content.getSelectionEnd();

                if (temp.length() > num) {
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionStart;
                    et_content.setText(s);
                    et_content.setSelection(tempSelection);
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_comments, menu);
        return true;
    }

    class MyNetworkCallBack extends NetworkCallBack {
        public void onNetworkCallBack(int id, boolean success, Object attach1, Object attach2) {
            if (!success) {
                UIHelper.showConfirmDialog(MyCommentsActivity.this, null, getResources().getString(R.string.server_busy));
                return;
            }
            switch(id) {
            case NetworkImpl.NET_WORK_ID_PUBLISH_COMMENTS:
                UIHelper.showConfirmDialog(MyCommentsActivity.this, null, getResources().getString(R.string.comments_public_success));
                et_content.setText("");
                break;
            }
        }
    }
}
